package com.hjxr.xmgl.web.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hjxr.xmgl.EventFunction.FunctionFactory;
import com.hjxr.xmgl.web.domain.EventQuotaFunctionQueue;
import com.hjxr.xmgl.web.domain.EvnetHistory;
import com.hjxr.xmgl.web.domain.ProjectEarlyWarningResult;
import com.hjxr.xmgl.web.domain.QuotaEarlyWarningInfos;
import com.hjxr.xmgl.web.mapper.EventQuotaFunctionQueueMapper;
import com.hjxr.xmgl.web.mapper.EvnetHistoryMapper;
import com.hjxr.xmgl.web.mapper.ProjectEarlyWarningResultMapper;
import com.hjxr.xmgl.web.mapper.QuotaEarlyWarningInfosMapper;
import com.hjxr.xmgl.web.service.IEventQuotaFunctionQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 数据函数运算队列Service业务层处理
 *
 * @author hjxr
 * @date 2021-12-02
 */
@Service
public class EventQuotaFunctionQueueServiceImpl implements IEventQuotaFunctionQueueService
{
    @Autowired
    private EventQuotaFunctionQueueMapper eventQuotaFunctionQueueMapper;

    @Autowired
    private QuotaEarlyWarningInfosMapper quotaEarlyWarningInfosMapper;

    @Autowired
    private EvnetHistoryMapper evnetHistoryMapper;

    @Autowired
    private ProjectEarlyWarningResultMapper projectEarlyWarningResultMapper;

    @Autowired
    private FunctionFactory functionFactory;

    /**
     * 查询数据函数运算队列
     *
     * @param id 数据函数运算队列ID
     * @return 数据函数运算队列
     */
    @Override
    public EventQuotaFunctionQueue selectEventQuotaFunctionQueueById(String id)
    {
        return eventQuotaFunctionQueueMapper.selectEventQuotaFunctionQueueById(id);
    }

    /**
     * 查询数据函数运算队列列表
     *
     * @param eventQuotaFunctionQueue 数据函数运算队列
     * @return 数据函数运算队列
     */
    @Override
    public List<EventQuotaFunctionQueue> selectEventQuotaFunctionQueueList(EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        return eventQuotaFunctionQueueMapper.selectEventQuotaFunctionQueueList(eventQuotaFunctionQueue);
    }

    /**
     * 新增数据函数运算队列
     *
     * @param eventQuotaFunctionQueue 数据函数运算队列
     * @return 结果
     */
    @Override
    public int insertEventQuotaFunctionQueue(EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        return eventQuotaFunctionQueueMapper.insertEventQuotaFunctionQueue(eventQuotaFunctionQueue);
    }

    /**
     * 修改数据函数运算队列
     *
     * @param eventQuotaFunctionQueue 数据函数运算队列
     * @return 结果
     */
    @Override
    public int updateEventQuotaFunctionQueue(EventQuotaFunctionQueue eventQuotaFunctionQueue)
    {
        return eventQuotaFunctionQueueMapper.updateEventQuotaFunctionQueue(eventQuotaFunctionQueue);
    }

    /**
     * 批量删除数据函数运算队列
     *
     * @param ids 需要删除的数据函数运算队列ID
     * @return 结果
     */
    @Override
    public int deleteEventQuotaFunctionQueueByIds(String[] ids)
    {
        return eventQuotaFunctionQueueMapper.deleteEventQuotaFunctionQueueByIds(ids);
    }

    /**
     * 删除数据函数运算队列信息
     *
     * @param id 数据函数运算队列ID
     * @return 结果
     */
    @Override
    public int deleteEventQuotaFunctionQueueById(String id)
    {
        return eventQuotaFunctionQueueMapper.deleteEventQuotaFunctionQueueById(id);
    }

    /**
     * 定时执行预警函数
     * @param EventQuotaFunctionQueue
     */
    @Transactional
    @Override
    public void execute(EventQuotaFunctionQueue EventQuotaFunctionQueue) {
        // 查找关于预警的相关描述
        String field =EventQuotaFunctionQueue.getDistTable().trim()+"."+EventQuotaFunctionQueue.getDistField().trim();
        QuotaEarlyWarningInfos quotaEarlyWarningInfos =quotaEarlyWarningInfosMapper.selectQuotaEarlyWarningInfosByField(field);
        //   写入执行列表(evnet_history)
        String sql = "update "+EventQuotaFunctionQueue.getDistTable() +
                " set " + EventQuotaFunctionQueue.getDistField() + " = " +
                functionFactory.getEvent(EventQuotaFunctionQueue.getFunctionName()).EventFunction(EventQuotaFunctionQueue.getParam1(),EventQuotaFunctionQueue.getParam2())
                + " where p_id = "+EventQuotaFunctionQueue.getProjectId() ;
        EvnetHistory evnetHistory = new EvnetHistory();
        evnetHistory.setExecuteSql(sql);
        evnetHistoryMapper.insertEvnetHistory(evnetHistory);
        // 写入预警信息表(project_early_warning_result)
        ProjectEarlyWarningResult projectEarlyWarningResult = new ProjectEarlyWarningResult();
        projectEarlyWarningResult.setField(EventQuotaFunctionQueue.getDistField());
        projectEarlyWarningResult.setModel(EventQuotaFunctionQueue.getDistTable());
        projectEarlyWarningResult.setProjectId(EventQuotaFunctionQueue.getProjectId());
        projectEarlyWarningResult.setRow(EventQuotaFunctionQueue.getRowId());
        if(quotaEarlyWarningInfos !=null){
            JSONArray jsonArray = JSONArray.parseArray(quotaEarlyWarningInfos.getEqRangeJson());
            projectEarlyWarningResult.setTips(jsonArray.getJSONObject(0).getString("tips"));
            projectEarlyWarningResult.setTipsColor(jsonArray.getJSONObject(0).getString("tipsColor"));
        }
        projectEarlyWarningResultMapper.insertProjectEarlyWarningResult(projectEarlyWarningResult);
        //   清理(event_quota_function_queue)表信息
//        eventQuotaFunctionQueueMapper.deleteEventQuotaFunctionQueueById(EventQuotaFunctionQueue.getId());
    }
}
